﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IPProvider.Service;
using System.Net;

namespace IPProvider
{
    public partial class FrmIPProvider : Form
    {
        public FrmIPProvider()
        {
            InitializeComponent();
        }

        /// <summary>
        /// IP信息 集合
        /// </summary>
        private IDictionary<string, IPInfo> dicInfo = null;
        /// <summary>
        /// 文本赋值委托
        /// </summary>
        /// <param name="text"></param>
        public delegate void SetTextCallback(string text);
        /// <summary>
        /// 序号
        /// </summary>
        private int seq = 1;

        /// <summary>
        /// 当前正在切换的 cob的索引值
        /// </summary>
        private int CurrentChangedSelectedIndex = -1;

        private void Form1_Load(object sender, EventArgs e)
        {

            this.rdoMin15.Checked = false;
            this.rdoMin30.Checked = true;
            this.rdoMin45.Checked = false;
            this.rdoMin60.Checked = false;
            this.rdoZdy.Checked = false;
            CboInit();
            if (this.cboIpNames.Items.Count > 0)
            {
                this.cboIpNames.SelectedIndex = 0;
            }
            this.txtAdressDefault.Text = this.txtIPAddress.Text;

        }
        /// <summary>
        /// 下拉框初始化
        /// </summary>
        private void CboInit()
        {
            this.cboIpNames.Items.Clear();
            dicInfo = IPProviders.GetIPInfo();
            this.cboIpNames.DisplayMember = "ConnectionName";
            this.cboIpNames.ValueMember = "Id";
            foreach (IPInfo ipInfo in dicInfo.Values)
            {
               this.cboIpNames.Items.Add(ipInfo);//ipInfo.ConnectionName
            }
            
            if (this.CurrentChangedSelectedIndex >= 0 && this.CurrentChangedSelectedIndex<=this.cboIpNames.Items.Count)
            {
                this.cboIpNames.SelectedIndex = this.CurrentChangedSelectedIndex;
            }
            else
            {
                if (this.cboIpNames.Items.Count > 0)
                {
                    this.cboIpNames.SelectedIndex = 0;
                }
            }
            
        }

        private void rdoMin15_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rdo = sender as RadioButton;
            if (rdo.Checked)
            {
                this.timChange.Interval = Convert.ToInt32(rdo.Text.Trim()) * 60 * 1000;
                this.timChange.Enabled = true;
            }


        }
        private void txtMin_TextChanged(object sender, EventArgs e)
        {
            if (this.txtMin.Text != "")
            {
                try
                {
                    int i = Convert.ToInt32(this.txtMin.Text.Trim());
                    if (i > 0)
                    {
                        this.timChange.Interval = i * 60 * 1000;
                    }
                    this.timChange.Enabled = true;
                }
                catch (Exception)
                {
                    this.txtMin.Text = "1";
                }
            }
        }

        private void cboIpNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            IPInfo ipInfo = this.cboIpNames.SelectedItem as IPInfo;
            if (ipInfo == null)
            {
                return;
            }
            this.txtSubmask.Text = ipInfo.Submask;
            this.txtWay.Text = ipInfo.GetWay;
            this.txtDNS.Text = ipInfo.DNS;
            this.txtIPAddress.Text = ipInfo.IPAddress;
            this.txtDNSBak.Text = ipInfo.DNSBak;
            this.txtMac.Text = ipInfo.Mac;
            this.txtIPName.Text = ipInfo.IPName;
            this.CurrentChangedSelectedIndex = this.cboIpNames.SelectedIndex;
        }

        private void btnProvider_Click(object sender, EventArgs e)
        {
            ChangeIP();
        }
        /// <summary>
        /// 切换 IP
        /// </summary>
        private void ChangeIP()
        {
            string ipAdress = GetIpAdress(this.txtIPAddress.Text);
            string connectionName = this.cboIpNames.Text;
            IPProviders.SetIPAddress(ipAdress, this.txtSubmask.Text, this.txtIPName.Text);
            CboInit();
            this.SetText("当前" + connectionName + "IP为" + this.txtIPAddress.Text);
        }
        /// <summary>
        /// 随机生成IP
        /// </summary>
        /// <param name="ipAdressSource"></param>
        /// <returns></returns>
        private string GetIpAdress(string ipAdressSource)
        {
            string ipAdress = string.Empty;
            Random rd = new Random();
            int value = rd.Next(255);

            if (!string.IsNullOrEmpty(ipAdressSource))
            {
                while (value <= 0)
                {
                    value = rd.Next(255);
                }
                string[] str = ipAdressSource.Split('.');
                if (str.Length == 4)
                {
                    str[3] = value.ToString();
                    ipAdress = string.Join(".", str);
                }
            }
            return ipAdress;
        }

        private void timChange_Tick(object sender, EventArgs e)
        {
            ChangeIP();
        }

        private void btnRefurbish_Click(object sender, EventArgs e)
        {
            CboInit();
        }

        private void btnChangeDefault_Click(object sender, EventArgs e)
        {
            string ipAdress =this.txtAdressDefault.Text;
            string connectionName = this.cboIpNames.Text;
            IPProviders.SetIPAddress(ipAdress, this.txtSubmask.Text, this.txtIPName.Text);
            CboInit();
            this.SetText("当前" + connectionName + "IP为" + this.txtIPAddress.Text);
        }

        /// <summary>
        /// 根据 连接名称，获取IP
        /// </summary>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        private string GetIp(string connectionName)
        {
            string result = string.Empty;
            if (dicInfo!=null)
            {
                foreach (IPInfo ipInfo in dicInfo.Values)
                {
                    if (ipInfo.ConnectionName==connectionName)
                    {
                        result = ipInfo.IPAddress;
                        break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        ///  给值
        /// </summary>
        /// <param name="text"></param>
        private void SetText(string text)
        {
            if (this.rtbResult.InvokeRequired)
            {
                SetTextCallback set = new SetTextCallback(SetText);
                this.Invoke(set, text);
            }
            else
            {
                this.rtbResult.AppendText(seq++ + "." + DateTime.Now + " " + text+Environment.NewLine);
                this.rtbResult.ScrollToCaret();
            }
        }
    }
}
